<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>boolean(布尔)数据类型 | ElasticSearch 7.7 权威指南中文版</title>
	<meta name="keywords" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <meta name="description" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
	<script>
	var _link = 'boolean.html';
    </script>
</head>
<body>
<div class="main-container">
    <section id="content">
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.7/boolean.html" rel="nofollow" target="_blank">https://www.elastic.co/guide/en/elasticsearch/reference/7.7/boolean.html</a>, 原文档版权归 www.elastic.co 所有<br/>本地英文版地址: <a href="../en/boolean.html" rel="nofollow" target="_blank">../en/boolean.html</a></div>
                        <!-- start body -->
                  <div class="page_header">
<strong>重要</strong>: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html" rel="nofollow">当前版本文档</a>。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch 权威指南 [7.7]</a></span>
»
<span class="breadcrumb-link"><a href="mapping.html">映射</a></span>
»
<span class="breadcrumb-link"><a href="mapping-types.html">字段数据类型</a></span>
»
<span class="breadcrumb-node">boolean(布尔)数据类型</span>
</div>
<div class="navheader">
<span class="prev">
<a href="binary.html">« binary(二进制)数据类型</a>
</span>
<span class="next">
<a href="date.html">date(日期)数据类型 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="boolean"></a>boolean(布尔)数据类型
</h2>
</div></div></div>

<p>布尔字段接受JSON <code class="literal">true</code>和<code class="literal">false</code>值，但也可以接受被解释为 true 或 false 的字符串：</p>
<div class="informaltable">
<table border="0" cellpadding="4px">
<colgroup>
<col>
<col>
</colgroup>
<tbody valign="top">
<tr>
<td valign="top">
<p>
false 值
</p>
</td>
<td valign="top">
<p>
<code class="literal">false</code>, <code class="literal">"false"</code>, <code class="literal">""</code> (空字符串)
</p>
</td>
</tr>
<tr>
<td valign="top">
<p>
true 值
</p>
</td>
<td valign="top">
<p>
<code class="literal">true</code>, <code class="literal">"true"</code>
</p>
</td>
</tr>
</tbody>
</table>
</div>
<p>例如：</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT my_index
{
  "mappings": {
    "properties": {
      "is_published": {
        "type": "boolean"
      }
    }
  }
}

POST my_index/_doc/1
{
  "is_published": "true" <a id="CO291-1"></a><i class="conum" data-value="1"></i>
}

GET my_index/_search
{
  "query": {
    "term": {
      "is_published": true <a id="CO291-2"></a><i class="conum" data-value="2"></i>
    }
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/621.console"></div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO291-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>用<code class="literal">"true"</code>对文档进行索引，这被解释为<code class="literal">true</code>。</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO291-2"><i class="conum" data-value="2"></i></a></p>
</td>
<td align="left" valign="top">
<p>搜索值为JSON <code class="literal">true</code>的文档。</p>
</td>
</tr>
</table>
</div>
<p>
像<a class="xref" href="search-aggregations-bucket-terms-aggregation.html" title="terms 聚合"><code class="literal">terms</code>聚合</a>这样的聚合使用<code class="literal">1</code>和<code class="literal">0</code>作为键，使用字符串<code class="literal">"true"</code>和<code class="literal">"false"</code>作为<code class="literal">key_as_string</code>。

布尔字段在脚本中使用时，返回<code class="literal">1</code>和<code class="literal">0</code>：
</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">POST my_index/_doc/1
{
  "is_published": true
}

POST my_index/_doc/2
{
  "is_published": false
}

GET my_index/_search
{
  "aggs": {
    "publish_state": {
      "terms": {
        "field": "is_published"
      }
    }
  },
  "script_fields": {
    "is_published": {
      "script": {
        "lang": "painless",
        "source": "doc['is_published'].value"
      }
    }
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/622.console"></div>
<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="boolean-params"></a><code class="literal">boolean</code>字段的参数
</h3>
</div></div></div>
<p><code class="literal">boolean</code>字段接受以下参数：</p>
<div class="informaltable">
<table border="0" cellpadding="4px">
<colgroup>
<col>
<col>
</colgroup>
<tbody valign="top">
<tr>
<td valign="top">
<p>
<a class="xref" href="mapping-boost.html" title="boost"><code class="literal">boost</code></a>
</p>
</td>
<td valign="top">
<p>
映射字段级查询时 提升(boost)。接受一个浮点数，默认值为 <code class="literal">1.0</code>。
</p>
</td>
</tr>
<tr>
<td valign="top">
<p>
<a class="xref" href="doc-values.html" title="doc_values"><code class="literal">doc_values</code></a>
</p>
</td>
<td valign="top">
<p>
是否应该以列跨度(column-stride)的方式将字段存储在磁盘上，以便以后用于排序、聚合或编写脚本? 接受<code class="literal">true</code> (默认)和<code class="literal">false</code>。
</p>
</td>
</tr>
<tr>
<td valign="top">
<p>
<a class="xref" href="mapping-index.html" title="index"><code class="literal">index</code></a>
</p>
</td>
<td valign="top">
<p>
字段是否可以搜索？接受<code class="literal">true</code>(默认)和<code class="literal">false</code>。
</p>
</td>
</tr>
<tr>
<td valign="top">
<p>
<a class="xref" href="null-value.html" title="null_value"><code class="literal">null_value</code></a>
</p>
</td>
<td valign="top">
<p>
接受上面列出的任何 true 或 false 值。

该值将替换任何显式的<code class="literal">null</code>值。

默认为<code class="literal">null</code>，这意味着该字段被视为缺失。
</p>
</td>
</tr>
<tr>
<td valign="top">
<p>
<a class="xref" href="mapping-store.html" title="store"><code class="literal">store</code></a>
</p>
</td>
<td valign="top">
<p>
字段值是否应该与<a class="xref" href="mapping-source-field.html" title="_source字段" rel="nofollow"><code class="literal">_source</code></a>字段分开存储和检索。

接受<code class="literal">true</code>和<code class="literal">false</code>(默认)。
</p>
</td>
</tr>
<tr>
<td valign="top">
<p>
<a class="xref" href="mapping-field-meta.html" title="meta"><code class="literal">meta</code></a>
</p>
</td>
<td valign="top">
<p>
与该字段有关的元信息。
</p>
</td>
</tr>
</tbody>
</table>
</div>
</div>

</div>
<div class="navfooter">
<span class="prev">
<a href="binary.html">« binary(二进制)数据类型</a>
</span>
<span class="next">
<a href="date.html">date(日期)数据类型 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>